package gov.mogaha.ntis.cmm.mob.util;

import gov.mogaha.ntis.cmm.mob.R;

import java.io.File;
import java.io.FileOutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;

public class Image {

	private final Context mContext;
	private final static int DEFAULT_IMAGE_SIZE=8;
	
	public Image(Context context){
		mContext = context;
	}
	
	public String saveImage(Bitmap bm) {
		String path = Environment.getExternalStorageDirectory().getAbsolutePath();
		File file = null;
		File file2 = null;

		if (bm != null) {
			try {
				file = new File(path + mContext.getString(R.string.ntis_mob));
				file.mkdir();
				file2 = new File(path + mContext.getString(R.string.ntis_mob) + java.lang.System.currentTimeMillis() + ".jpeg");

				FileOutputStream fos = new FileOutputStream(file2);

				if (fos != null) {
					bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
					fos.close();
				}
			} catch (Exception e) {
				Logger.e("Image save fail");
			}
		}
		
		return file2.getPath();
	}	
	
	
	public String resizeSaveImage(Bitmap bm) {
		Bitmap resizeBm = resizeImage(bm);
		String path = Environment.getExternalStorageDirectory().getAbsolutePath();
		File file = null;
		File file2 = null;

		if (resizeBm != null) {
			try {
				file = new File(path + mContext.getString(R.string.ntis_mob));
				file.mkdir();
				file2 = new File(path + mContext.getString(R.string.ntis_mob) + java.lang.System.currentTimeMillis() + ".jpeg");

				FileOutputStream fos = new FileOutputStream(file2);

				if (fos != null) {
					resizeBm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
					fos.close();
				}
			} catch (Exception e) {
				Logger.e("Image save fail");
			}
		}
		
		return file2.getPath();
	}
	
	public Bitmap resizeImage(Bitmap bm, int value){

		
		int width = bm.getWidth();
		int height = bm.getHeight();
		int fixwidth = width*value/height;
		int fixheight = value;
		
		return Bitmap.createScaledBitmap(bm, fixwidth, fixheight, true);
	}
	
	public Bitmap resizeImage(Bitmap bm){
		
		int width = bm.getWidth();
		int height = bm.getHeight();
		int fixwidth = width*DEFAULT_IMAGE_SIZE/height;
		int fixheight = DEFAULT_IMAGE_SIZE;
		
		return Bitmap.createScaledBitmap(bm, fixwidth, fixheight, true);
	}
}
